/**
 * 项目初始化时需要执行的事件，在app.vue中引用
 */
import { setThemeColor } from '@/utils/color/theme'
import { addClass } from '@/utils/domUtil'
import { setDevice, getDevice } from '@/utils/device'
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
export const useInit = () => {
  const appStore = useAppStoreWithOut()
  const { systemConfig } = storeToRefs(appStore)
  const updateDevice = () => {
    systemConfig.value.device = getDevice()
    appStore.setSystemConfigCache()
    setDevice(systemConfig.value.device)
  }
  const appInit = () => {
    appStore.getSystemConfigCache()
    setThemeColor(systemConfig.value)
    updateDevice()
    addEventListener('resize', updateDevice)
    const htmlElement = document.querySelector('html') as HTMLHtmlElement | null
    if (htmlElement) {
      systemConfig.value.isGreyPatternEnable && addClass(htmlElement, 'html-grey')
      systemConfig.value.isColorWeaknessPatternEnable && addClass(htmlElement, 'html-color-weakness')
    }
  }
  return {
    appInit
  }
}
